package dLogistic.domain.model.Paczka;

import dLogistic.infrastructure.persistence.hibernate.HibernateUtil;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import java.util.List;
import java.util.UUID;

/**
 * Created by Przemek on 2014-10-23.
 */
@Entity
@Table(name = "TrasaPaczki")
public class TrasaPaczki {
    private static SessionFactory factory;

    @Id
    private String uuid;

    private String uuid_paczka;
    private String uuid_kurier;

    private String RoadStart; //UUID
    private String RoadEnd; //UUID

    private String DataStart;
    private String DataEnd;

    private Integer typ; // 0 - start, 1- wszystko co po drodze, 2 - koniec
    private Integer kolejnosc;

    public TrasaPaczki(String uuid_paczka, String RoadStart, String RoadEnd, Integer typ, Integer kolejnosc){
        this.uuid = UUID.randomUUID().toString();
        this.uuid_paczka = uuid_paczka;
        this.RoadStart = RoadStart;
        this.RoadEnd = RoadEnd;
        this.typ = typ;
        this.kolejnosc = kolejnosc;

        this.uuid_kurier = "";
        this.DataStart = "";
        this.DataEnd = "";

    }

    public Integer typ(){
        return typ;
    }
    public Integer kolejnosc(){
        return kolejnosc;
    }
    public String RoadStart(){
        return RoadStart;
    }

    public String RoadEnd(){
        return RoadEnd;
    }
    public String DataEnd(){
        return DataEnd;
    }
    public String DataStart(){
        return DataStart;
    }

    public String uuid_paczka(){
        return uuid_paczka;
    }

    public String uuid_kurier(){
        return  uuid_kurier;
    }

    public void ustawDataStart(String dtStart){
        DataStart = dtStart;
    }

    public void ustawDataEnd(String dtEnd){
        DataEnd = dtEnd;
    }

    public TrasaPaczki znajdzTrase(String uuid_paczka, String przekazujacy, String odbierajacy){
        factory = HibernateUtil.getSessionFactory();
        Session session = factory.getCurrentSession();
        Transaction transaction = session.beginTransaction();

        String sql = "select * from TrasaPaczki where uuid_paczka = :uuid_paczka";
        sql = sql + " and (RoadStart = :przekazujacy or RoadEnd = :odbierajacy)";
        SQLQuery query = session.createSQLQuery(sql);
        query.addEntity(TrasaPaczki.class);
        query.setParameter("uuid_paczka", uuid_paczka);
        query.setParameter("przekazujacy", przekazujacy);
        query.setParameter("odbierajacy", odbierajacy);
        List results = query.list();
        transaction.rollback();
        if (results.isEmpty()){
            return null;
        }
        else{
            return (TrasaPaczki) results.get(0);
        }
    }

    public String uuid(){
        return uuid;
    }

    public TrasaPaczki(){

    }
}
